package com.njth.util;

import java.sql.Timestamp;

import java.util.Calendar;
import java.util.Date;

/**
 * 系统名：网络系统 功能概要: 数据与日期之间的转换 完成者:hanbing 完成日:
 */
public class DateFormatter {
	/**
	 * date转换为string形式
	 * 
	 * @param date
	 * @return string(yyyy-MM-dd)
	 */
	public static String DateToString(Date date) {
		java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		String strDateTime = "";

		if (date != null) {
			strDateTime = formater.format(date);
		}

		return strDateTime;
	}

	/**
	 * date转换为长string形式
	 * 
	 * @param date
	 * @return string(yyyy-MM-dd HH:mm:ss)
	 */
	public static String DateToLongString(Date date) {
		java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String strDateTime = "";

		if (date != null) {
			strDateTime = formater.format(date);
		}

		return strDateTime;
	}

	/**
	 * 指定的年、月、日转换为date
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return date
	 */
	public static Date YmdToDate(int year, int month, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);

		return calendar.getTime();
	}

	/**
	 * date转换为sting
	 * 
	 * @param date
	 * @return string（MM/dd hh:mm）
	 */
	public static String communityDateToString(Date date) {
		java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(
				"MM/dd hh:mm");
		String strDateTime = formater.format(date);

		return strDateTime;
	}

	/**
	 * string转换为date
	 * 
	 * @param str
	 * @return date(yyyy-MM-dd)
	 */
	public static Date StringToDate(String str) {
		Date dateTime = null;

		try {
			if (!((str == null) || str.equals(""))) {
				java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(
						"yyyy-MM-dd");
				dateTime = formater.parse(str);
			}
		} catch (Exception ex) {
			System.err.println(ex.toString());
		}

		return dateTime;
	}

	/**
	 * string转换为Timestamp
	 * 
	 * @param str
	 * @return timestamp
	 * @throws Exception
	 */
	public static Timestamp StringToDateHMS(String str) throws Exception {
		Timestamp time = null;

		try {
			time = java.sql.Timestamp.valueOf(str);
		} catch (Exception ex) {
			System.err.println(ex.toString());
		}

		return time;
	}

	/**
	 * 取得一日内的最小时间
	 * 
	 * @param date
	 * @return date
	 */
	public static Date getMinDateOfDay(Date date) {
		if (date == null) {
			return null;
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, calendar
				.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar
				.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar
				.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar
				.getActualMinimum(Calendar.MILLISECOND));

		return calendar.getTime();
	}

	/**
	 * 取得一日内的最大时间
	 * 
	 * @param date
	 * @return date
	 */
	public static Date getMaxDateOfDay(Date date) {
		if (date == null) {
			return null;
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, calendar
				.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar
				.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar
				.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar
				.getActualMaximum(Calendar.MILLISECOND));

		return calendar.getTime();
	}

	/**
	 * date转换为指定的格式
	 * 
	 * @param date
	 * @param pattern
	 * @return string
	 * @throws Exception
	 */
	public static String DateToString(Date date, String pattern)
			throws Exception {
		String strDateTime = null;

		try {
			java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(
					pattern);
			strDateTime = formater.format(date);
		} catch (Exception ex) {
			throw ex;
		}

		return strDateTime;
	}

	/**
	 * STING TO LONG
	 * 
	 * @param date
	 *            String data
	 * @return long
	 */
	public static long StringToLong(String date) {
		Date dateStr = DateFormatter.StringToDate(date);
		return dateStr.getTime();
	}

	/**
	 * Long To String
	 * 
	 * @param dateLong
	 * @return String data
	 */
	public static String LongToString(long dateLong) {
		Date date = new Date(dateLong);
		return DateFormatter.DateToString(date);
	}
}
